import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import data.Data;
import graph.GraphPanel;
import graph.color.Color;
import graph.element.shape.TypeForm;


@SuppressWarnings("serial")
public class JScalePanel extends GraphPanel{
	
	public Graphics g;
	public GraphPanel graph;
	public ArrayList<Integer> liste;
	private int nb;
	
	public JScalePanel(Data data) {
		super(data);
		this.data = genData(data);
	}
	
	@SuppressWarnings("unchecked")
	public Data genData(Data data) {
		liste = new ArrayList<Integer>();
		nb = 10;
		ArrayList<Integer> listeVal = new ArrayList<Integer>();
		for (HashMap<String, Integer> s : data.getData().values()) {
			for (int u : s.values()) {
				if (!listeVal.contains(u))
					listeVal.add(u);
			}
		}
		if (listeVal.size()>nb) {
			ArrayList<Integer> temp = (ArrayList<Integer>) listeVal.clone();
			Collections.sort(temp);
			for (int i : temp) {
				System.out.print(i + " ");
			}
			System.out.println();
			liste.add(temp.get(0));
			int taux = (int) Math.floor(listeVal.size()/nb);
			System.out.println("taux " + taux);
			int nbT;
			int nbR = nb-2;
			for (int p=1; p<temp.size()-1; p=p+taux) {
				nbT = temp.size()-1-p;
				if (nbT==nbR && nbT>0) {
					liste.add(temp.get(p));
					nbR--;
				}
				else if (nbR>0) {
					int random = (int)(Math.random()*2);
					if (random==1) {
						liste.add(temp.get(p));
						nbR--;
					}					
				}
				else {
					break;
				}
			}
			liste.add(temp.get(temp.size()-1));			
		}
		else {
			liste = (ArrayList<Integer>) listeVal.clone();
			Collections.sort(liste);
		}
		
		Data dataEchelle = new Data();
		HashMap<String,HashMap<String, Integer>> donnee = new HashMap<String,HashMap<String, Integer>>();
		System.out.println("ici" + liste.size());
		System.out.println(liste.toString());
		int max = Collections.max(liste);
		int nbMax = Integer.toString(max).length();
		for (int i : liste) {
			String dimL = "";
			String dimC = Integer.toString(i);
			while (dimC.length()<nbMax)
				dimC = "0"+ dimC;
			int val = i;
			if (val > max) {
				max = val;
			}
			if (!donnee.containsKey(dimL)) {
				donnee.put(dimL, new HashMap<String,Integer>());
			}
			donnee.get(dimL).put(dimC, val);
		}
		dataEchelle.setData(donnee, max);
		return dataEchelle;		
	}
	
	public void modifierParam(boolean chg, Data data, TypeForm t, boolean b, Color c) {
    	if (chg)
    		this.data = genData(data);
    	this.t = t;
    	this.b = b;
    	this.c = c;
    }
}
